package org.meb.conquest.web.ctrl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import lombok.Getter;
import lombok.Setter;

import org.meb.conquest.db.dao.Query;
import org.meb.conquest.db.model.Deck;
import org.meb.conquest.web.auth.WebUser;
import org.meb.conquest.web.model.DeckItem;
import org.meb.conquest.web.services.CardService;
import org.meb.conquest.web.services.DeckService;
import org.meb.conquest.web.services.SearchService;

@Named
@ViewScoped
@Getter
@Setter
public class DeckListCtrl implements Serializable {

	private static final long serialVersionUID = -4308071997837944252L;

	@Inject
	private SearchService searchService;

	@Inject
	private CardService cardService;

	@Inject
	private DeckService deckService;

	@Inject
	private WebUser webUser;

	private List<DeckItem> deckItems;

	@PostConstruct
	public void initialize() {

		if (webUser.isNotSignedIn()) {
			throw new IllegalStateException("Not signed in");
		}

		Deck deck = new Deck();
		deck.setUserId(webUser.getUserId());
		Query<Deck> query = new Query<Deck>(deck);
		List<Deck> decks = searchService.find(query);
		deckItems = new ArrayList<>();
		for (Deck d : decks) {
			deckItems.add(new DeckItem(d));
		}
	}
}
